package com.example.ui.contact.db;

import java.util.Collections;
import java.util.LinkedList;

import com.example.ui.contact.bean.BaseItem;
import com.example.ui.contact.bean.MeItem;
import com.example.ui.contact.bean.RequestItem;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class RequestMsgDB {
	public static final String MSG_DBNAME = "Msg.db";
	private static final String RequestMsg_TABLE_NAME = "requestMsg";
	private SQLiteDatabase db;
	@SuppressWarnings("static-access")
	public RequestMsgDB(Context context){
		db = context.openOrCreateDatabase(MSG_DBNAME,Context.MODE_APPEND,null);
		db.execSQL("CREATE table IF NOT EXISTS "
				+ RequestMsg_TABLE_NAME
				+ " (RequestMsg_TABLE_NAME INTEGER PRIMARY KEY AUTOINCREMENT,typeId TEXT, userId TEXT," +
				"mobile TEXT,nickname TEXT,tokenType TEXT,pic TEXT)");
                //gender TEXT,   company TEXT,  typeJob TEXT ,  occupation TEXT,  supply TEXT,requirement TEXT,

		
	}


	public void saveRequestMsg(RequestItem item){
		if(isExist(item.getUserId())){
			ContentValues cv = new ContentValues();
			cv.put("typeId", item.getType());
			cv.put("userId", item.getUserId());
			cv.put("mobile", item.getMobile());
			cv.put("nickname",item.getNick());
			cv.put("tokenType",item.getTokenType());
			cv.put("pic", item.getPic());
			db.update(RequestMsg_TABLE_NAME, cv, "userId=?",
					new String[] { item.getUserId() });			
		}else{
			
			db.execSQL(
					"insert into "
							+ RequestMsg_TABLE_NAME
							+ " (typeId,userId,mobile,nickname,tokenType,pic) "
							+"values(?,?,?,?,?,?)",
							new Object[] { item.getType(), item.getUserId(),item.getMobile(),item.getNick(),item.getTokenType(), item.getPic()});
		}
		//typeJob,occupation,supply,requirement,
         //gender,--- item.getGender(),  company, ---  item.getCompany(), item.getTypeJob(), item.getOccupation(),item.getSupply(),item.getRequirement(),
	
	}

	public LinkedList<RequestItem> getRequestMsgList() {
		LinkedList<RequestItem> list = new LinkedList<RequestItem>();
		Cursor c = db.rawQuery("SELECT * from " + RequestMsg_TABLE_NAME, null);
		
		while (c.moveToNext()) {
			String typeId = c.getString(c.getColumnIndex("typeId"));
			String userId = c.getString(c.getColumnIndex("userId"));
			String mobile = c.getString(c.getColumnIndex("mobile"));
			String nickname = c.getString(c.getColumnIndex("nickname"));
			//String gender = c.getString(c.getColumnIndex("gender"));
			//String company = c.getString(c.getColumnIndex("company"));
			//String typeJob = c.getString(c.getColumnIndex("typeJob"));
			//String occupation = c.getString(c.getColumnIndex("occupation"));
			//String supply = c.getString(c.getColumnIndex("supply"));
			//String requirement = c.getString(c.getColumnIndex("requirement"));
			String tokenType = c.getString(c.getColumnIndex("tokenType"));
			String pic = c.getString(c.getColumnIndex("pic"));
			RequestItem item = new RequestItem();
			item.setType(typeId);
			item.setUserId(userId);
			item.setMobile(mobile);
			item.setNick(nickname);
			//item.setGender(gender);
			//item.setCompany(company);
			//item.setTypeJob(typeJob);
			//item.setOccupation(occupation);					
			//item.setSupply(supply);
			//item.setRequirement(requirement);	
			item.setTokenType(tokenType);
			item.setPic(pic);
			list.add(item);
		}
		//Collections.sort(list);// 按时间降序
		return list;
	}
	
	public void close() {
		if (db != null)
			db.close();
	}
	
	
	private boolean isExist(String userId) {
		Cursor c = db.rawQuery("SELECT * FROM " + RequestMsg_TABLE_NAME
				+ " WHERE userId = ?", new String[] { userId });
		return c.moveToFirst();
	}	
	
}
